//
//  NewFeatureCollectionViewController.swift
//  Weibo
//
//  Created by 孙明海 on 16/1/26.
//  Copyright © 2016年 孙明海. All rights reserved.
//

import UIKit
import SnapKit

//可重用标示
private let NewFeatureCollectionID = "NewFeatureCollectionCell"
//新特性图片的数量
private let WBNewFeatureCount = 4

class NewFeatureCollectionViewController: UICollectionViewController {

    init() {
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = .Horizontal
        super.init(collectionViewLayout: layout)
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.bounces = false
        collectionView?.pagingEnabled = true
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.collectionView!.registerClass(NewFeatureCollectionViewCell.self, forCellWithReuseIdentifier: NewFeatureCollectionID)
    }
    //MARK: - 隐藏状态栏
    override func prefersStatusBarHidden() -> Bool {
        return true
    }

}

//MARK: - UICollectionViewControllerDataSource
extension NewFeatureCollectionViewController {
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return WBNewFeatureCount
    }
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(NewFeatureCollectionID, forIndexPath: indexPath) as! NewFeatureCollectionViewCell
        cell.index = indexPath.item
        return cell
    }
    
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        let page = Int(scrollView.contentOffset.x / scrollView.bounds.size.width)
        if page != WBNewFeatureCount - 1 {
            return
        }
        let cell = collectionView?.cellForItemAtIndexPath(NSIndexPath(forItem: page, inSection: 0)) as! NewFeatureCollectionViewCell
        cell.showStartButtonAnimation()
    }
}


private class NewFeatureCollectionViewCell: UICollectionViewCell {
    
    
    @objc private func clickStartButton() {
        print("开始体验")
        
        NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootViewControllerNotification, object: nil)
    }
    
    private var index: Int = 0 {
        didSet {
            iconView.image = UIImage(named: "new_feature_\(index + 1)")
            startButton.hidden = true
        }
    }
    
    private func showStartButtonAnimation() {
        startButton.hidden = false
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        startButton.userInteractionEnabled = false
        UIView.animateWithDuration(2,
            delay: 0,
            usingSpringWithDamping: 0.6,
            initialSpringVelocity: 10,
            options: [],
            animations: { () -> Void in
                self.startButton.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                self.startButton.userInteractionEnabled = true
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setUI() {
        addSubview(iconView)
        addSubview(startButton)
        
        startButton.addTarget(self, action: "clickStartButton", forControlEvents: .TouchUpInside)
        
        iconView.frame = bounds
        startButton.sizeToFit()
        startButton.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_bottom).multipliedBy(0.8)
        }
    }
    
    //MARK: - 懒加载控件
    private lazy var iconView: UIImageView = UIImageView()
    private lazy var startButton: UIButton = UIButton(title: "开始体验", color: UIColor.whiteColor(), backImageName: "new_feature_finish_button")
}